/**
 *起牌规则 
 */
package com.mj.scence.game
{
	import com.mj.scence.game.view.MJCard;
	import com.mj.vo.Card;

	public class TakeCardRule
	{
		private var column:int;//一行多少个牌位
		private var allCard_Other:Vector.<MJCard>;//别人是庄家的所有牌序
		private var allCard_Self:Vector.<MJCard>;//自己是庄家的所有牌序
		private var isSelfBanker:Boolean;
		private var myCardsInfo:Vector.<Card>;
		private var myCards:Vector.<MJCard>;
		private var currentTakeIndex:int;
		private var lessTakedCardNum:int;//还剩多少张牌没起
		private var isTopCard:Boolean;//是否起上层的牌
		
		public function TakeCardRule()
		{
			column = 16;
			myCards = new Vector.<MJCard>();
			isTopCard = true;
		}
		
		public function ini(allCardOther:Vector.<MJCard>, $isSelfBanker:Boolean):void
		{
			if(allCard_Other)
			{
				allCard_Other.length = 0;
				allCard_Other = null;
			}
			if(allCard_Self)
			{
				allCard_Self.length = 0;
				allCard_Self = null;
			}
			if(myCards)
			{
				myCards.length = 0;
			}
			lessTakedCardNum = 13;
			
			allCard_Other = allCardOther;
			allCard_Self = allCard_Other;//自己是庄家时，拿牌位置暂时与对方为庄家时一样
			isSelfBanker = $isSelfBanker;
		}
		
		
		public function iniMyCardsInfo($myCardsInfo:Vector.<Card>):void
		{
			myCardsInfo = $myCardsInfo;
		}
		
		public function addMyCards($myCard:MJCard):void
		{
			myCards.push($myCard);
		}
		
		//定义棋牌位置
		public function iniStartPos(pos:int):void
		{
			currentTakeIndex = pos;
			
		}
		
		
		//双方各获取一张牌,第一张为自己的牌
		public function takeIniCard():Vector.<MJCard>
		{
			if(lessTakedCardNum == 0)
				return null;
			var cardPare:Vector.<MJCard> = new Vector.<MJCard>();
			var myCard:MJCard;
			var otherCard:MJCard;
			var allCards:Vector.<MJCard>;
			if(isSelfBanker)
				allCards = allCard_Self;
			else
				allCards = allCard_Other;
			
			if(isSelfBanker)//自己先拿
			{
				myCard = allCards[column+currentTakeIndex];
				otherCard = allCards[currentTakeIndex];
			}
			else
			{
				otherCard = allCards[column+currentTakeIndex];
				myCard = allCards[currentTakeIndex];
			}
			
			cardPare.push(myCard);
			cardPare.push(otherCard);
			currentTakeIndex+=1;
			if(currentTakeIndex>=column*2)
				currentTakeIndex = 0;
			lessTakedCardNum--;
			return cardPare;
		}
		
		//起一张牌
		public function takeOneCard():MJCard
		{
			var beTakedCard:MJCard;
			var allCards:Vector.<MJCard>;
			if(isSelfBanker)
				allCards = allCard_Self;
			else
				allCards = allCard_Other;
			if(isTopCard)
			{
				beTakedCard = allCards[column+currentTakeIndex];
			}
			else
			{
				beTakedCard = allCards[currentTakeIndex];
				currentTakeIndex++;
				if(currentTakeIndex>=column*2)
					currentTakeIndex = 0
			}
			isTopCard = !isTopCard;
			return beTakedCard;
		}
		
		
	}
}